package cn.iocoder.yudao.module.member.controller.admin.vehicleinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.vehicleinfo.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.vehicleinfo.VehicleInfoDO;
import cn.iocoder.yudao.module.member.service.vehicleinfo.VehicleInfoService;

@Tag(name = "管理后台 - 车辆管理")
@RestController
@RequestMapping("/member/vehicle-info")
@Validated
public class VehicleInfoController {

    @Resource
    private VehicleInfoService vehicleInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建车辆管理")
    @PreAuthorize("@ss.hasPermission('member:vehicle-info:create')")
    public CommonResult<Long> createVehicleInfo(@Valid @RequestBody VehicleInfoSaveReqVO createReqVO) {
        return success(vehicleInfoService.createVehicleInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新车辆管理")
    @PreAuthorize("@ss.hasPermission('member:vehicle-info:update')")
    public CommonResult<Boolean> updateVehicleInfo(@Valid @RequestBody VehicleInfoSaveReqVO updateReqVO) {
        vehicleInfoService.updateVehicleInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除车辆管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:vehicle-info:delete')")
    public CommonResult<Boolean> deleteVehicleInfo(@RequestParam("id") Long id) {
        vehicleInfoService.deleteVehicleInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得车辆管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:vehicle-info:query')")
    public CommonResult<VehicleInfoRespVO> getVehicleInfo(@RequestParam("id") Long id) {
        VehicleInfoDO vehicleInfo = vehicleInfoService.getVehicleInfo(id);
        return success(BeanUtils.toBean(vehicleInfo, VehicleInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得车辆管理分页")
    @PreAuthorize("@ss.hasPermission('member:vehicle-info:query')")
    public CommonResult<PageResult<VehicleInfoRespVO>> getVehicleInfoPage(@Valid VehicleInfoPageReqVO pageReqVO) {
        PageResult<VehicleInfoDO> pageResult = vehicleInfoService.getVehicleInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VehicleInfoRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得车辆管理列表")
    public CommonResult<List<VehicleInfoRespVO>> getVehicleInfoList( VehicleInfoPageReqVO pageReqVO) {
        List<VehicleInfoDO> pageResult = vehicleInfoService.getVehicleInfoList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, VehicleInfoRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出车辆管理 Excel")
    @PreAuthorize("@ss.hasPermission('member:vehicle-info:export')")
    @OperateLog(type = EXPORT)
    public void exportVehicleInfoExcel(@Valid VehicleInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VehicleInfoDO> list = vehicleInfoService.getVehicleInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "车辆管理.xls", "数据", VehicleInfoRespVO.class,
                        BeanUtils.toBean(list, VehicleInfoRespVO.class));
    }

}